import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn } from '@delon/abc/st';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SupplyManagementService } from '../../services/supply-management.service';
import { SupplyManagementVehicleAssignedCarComponent } from '../assigned-car/assigned-car.component';
import { SupplyManagementUpdateExternalSnComponent } from '../update-external-sn/update-external-sn.component';

@Component({
  selector: 'app-supply-management-vehicle-detail',
  templateUrl: './vehicle-detail.component.html',
  styleUrls: ['./vehicle-detail.component.less']
})
export class SupplyManagementVehicleDetailComponent implements OnInit {

  id = this.route.snapshot.params.id;
  i: any;
  serviceType: any = {
    1: '抢单',
    2: '指派'
  }
  logColumns: STColumn[] = [
    { title: '内容', index: 'operationContent' },
    { title: '操作人', render: 'operator' },
    { title: '操作时间', index: 'operatorTimestamp' },
  ];
  totalObj: any;
  attObj: any;
  status: any = { 1: '待接单', 2: '已接单', 3: '已取消' };
  totalExpensePrice = 0;
  expenseColumns: STColumn[] = [
    {
      title: '款项',
      width: '150px',
      className: 'text-center',
      index: 'expenseName'
    },
    {
      title: '总费用(元)',
      width: '150px',
      className: 'text-center',
      render: 'total'
    },
    {
      title: '协议金额(元)',
      width: '150px',
      className: 'text-center',
      render: 'price'
    },
  ];
  get reqParams() {
    return { 
      operateObject: this.i?.resourceCode,
      operateTypeList:  [4,7],
     };
  }
  constructor(
    private route: ActivatedRoute,
    private msgSrv: NzMessageService,
    public service: SupplyManagementService,
    public modal: NzModalService,
    public router: Router
  ) {

  }

  ngOnInit(): void {
    this.getGoodsSourceDetail()
  }

  getGoodsSourceDetail() {
    this.service.request(this.service.$api_get_getCompleteVehicleDetail, { id: this.id }).subscribe(res => {
      console.log('888')
      console.log(this.i)
      const expenseList = res?.expenseVOList || [];
      this.totalExpensePrice = 0;
      this.attObj = res?.expenseVOList?.filter((data: any) => data.expenseCode === 'ATT')[0];
      this.totalObj = res?.expenseVOList?.filter((data: any) => data.expenseCode === 'TOTAL')[0];
      console.log(this.attObj)
      console.log(this.totalObj)
      expenseList.forEach((e: any) => {
        this.totalExpensePrice += e?.price * e?.rate;
      });
      this.i = { ...res, totalExpensePrice: this.totalExpensePrice };

    })
  }
  /**
   * 修改货源
   */
  updateGoodsSource(record: any) {
    this.router.navigate(['/supply-management/vehicle-amend', record.id], {
      queryParams: {
        sta: 1
      }
    });
  }

  /**
   * 取消货源
   */
  cancleGoodsSource() {
    this.modal.confirm({
      nzTitle: '<b>确定取消货源吗?</b>',
      nzContent: `<b>取消后不可恢复，谨慎操作</b>`,
      nzOnOk: () =>
      this.service.request(this.service.$api_cancle_goods_source, { id: this.id }).subscribe(res => {
        if (res === true) {
          this.service.msgSrv.success('操作成功！');
          this.getGoodsSourceDetail()
        }
      })
    })
  }


  /**
  * 编辑外部货源号
  * @param item st当前行对象
  */
  editEnternalSn(item: any) {
    const modalRef = this.modal.create({
      nzWidth: '400px',
      nzContent: SupplyManagementUpdateExternalSnComponent,
      nzComponentParams: {
        orderObject: item,
      },
      nzFooter: null,
      nzClosable: false
    });
  }

  /**
  * 重新指派
  */
   assignedCar(item: any) {
    const { id } = item;
    console.log(id)
    const modalRef = this.modal.create({
      nzTitle: '指派熟车',
      nzWidth: '1200px',
      nzContent: SupplyManagementVehicleAssignedCarComponent,
      nzComponentParams: {
        i: item,
        status: 'anew',
        params: { id },
        url: this.service.$api_save_assign_vehicle,
      },
      nzFooter: null,

    });
    modalRef.afterClose.subscribe((result) => {
      if (result) {
      }
    });
  }
  nextOrder(item: any) {
    this.router.navigate(['/supply-management/vehicle-next', item.id], {
      queryParams: {
        sta: 2
      }
    });
  }
  goBack() {
    window.history.go(-1);
  }

}
